package com.adcc.eoss.efb.ngctrl.Controller;

import com.adcc.eoss.efb.ngctrl.Service.ICmdService;
import com.adcc.eoss.efb.ngctrl.model.Nginx;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Controller
public class MainControler {

    @Autowired
    ICmdService cmdService;

    @GetMapping("/")
    public String index()
    {
        Properties props = System.getProperties();
        return "index";
    }

    @ResponseBody
    @PostMapping("/stopng")
    public String stopNg(@RequestBody Map<String,String> data) throws IOException {
        String pid = data.get("pid");
        String res = "{\"res\":\"success\"}";
        cmdService.stop(pid);
        return res;
    }

    @ResponseBody
    @PostMapping(value = "/reload",produces = "text/html")
    public String reload(@RequestBody Map<String,String> data) throws IOException, InterruptedException {
        FileUtils.writeStringToFile(new File(getConfFilePath(data.get("pid"))),data.get("conf"),false);
        String pStr = cmdService.getNgPath(data.get("pid"));
        String cStr = cmdService.getConf(data.get("pid"));
        cmdService.stop(data.get("pid"));
        Thread.sleep(1000);
        cmdService.start(pStr,cStr);
        return "success";
    }

    @ResponseBody
    @PostMapping("/startng")
    public String startng(@RequestBody Map<String,String> data) throws IOException {
        String res = "{\"res\":\"success\"}";
        cmdService.start(data.get("ngpath"),data.get("ngconf"));
        return res;
    }

    @GetMapping(value = "/conf")
    public String conf(@RequestParam String pid)
    {
        return "conf";
    }

    @ResponseBody
    @GetMapping(value = "/getconftxt",produces = "text/html")
    public String getConfTxt(@RequestParam String pid) throws IOException {
        String res = "";
        File conffile = new File(getConfFilePath(pid));
        res=org.apache.commons.io.FileUtils.readFileToString(conffile,"UTF-8");
        return res;
    }

    public String getConfFilePath(String pid) throws IOException {
        return cmdService.getConfFilePath(pid);
    }

    @ResponseBody
    @PostMapping(value = "/examConf" ,produces = "text/html")
    public String examConf(@RequestBody Map<String,String> data) throws IOException {
        String res = "";
        res = cmdService.examConf(data.get("ngpath"), data.get("ngconf"));
        return res;
    }

    @GetMapping(value = "/conferror")
    public String conferror(@RequestParam String ngpath,@RequestParam String ngconf)
    {
        return "conferror";
    }

    @ResponseBody
    @GetMapping(value = "/getconftxtByPath",produces = "text/html")
    public String getconftxtByPath(@RequestParam String ngpath, @RequestParam String ngconf) throws IOException {
        String res = "";
        File conffile = new File(ngpath + ngconf);
        res=org.apache.commons.io.FileUtils.readFileToString(conffile,"UTF-8");
        return res;
    }

    @ResponseBody
    @PostMapping(value = "/reloadByconfPath",produces = "text/html")
    public String reloadByconfPath(@RequestBody Map<String,String> data) throws IOException, InterruptedException {
        FileUtils.writeStringToFile(new File(data.get("ngpath")+data.get("ngconf")),data.get("conf"),false);
        cmdService.start(data.get("ngpath"),data.get("ngconf"));
        return "success";
    }

    @ResponseBody
    @GetMapping(value = "/getnglist",produces = "application/json")
    public List<Nginx> getNgList()
    {
        return cmdService.getNgList();
    }
}
